<?php

namespace App\Http\Controllers\Start;

use App\Http\Controllers\HandleRedis;
use App\Interfaces\ObServer;
use App\Libs\Timer;

/**
 * 处理机器人的事件
 * Class RobotController
 * @package App\Http\Controllers\Start
 */
class RobotController implements ObServer
{
    use \App\Traits\StartInitParam;//获取初始值
    use \App\Traits\StartHandles;//获取start常用的函数

    private static $randomIntoRoomTime = 2000;//概率时(概率越低提高入房几率)

    //进房
    private static $startRobotRandomIntoRoomTime = 20;//开始加入房间(初始值)(初始值越低提高调用频率)

    private static $successProbabilityIntoRoom = 0.5;//机器人成功进房概率(无限大0~99999999概率值)

    private static $failProbabilityIntoRoom = 0.5;//机器人失败进房概率(无限大0~99999999概率值)

    //下注
    private static $startRobotRandomBetRoomTime = 3;//开始下注时间

    private static $successProbabilityBetRoom = 0.1;//机器人成功投注概率(无限大0~99999999概率值)

    private static $failProbabilityBetRoom = 1;//机器人失败投注概率(无限大0~99999999概率值)

    /**
     * 用于注册类方法
     * @param $server
     * @param $parameter
     */
    public function onStartServer($server, $parameter)
    {
        //机器人自动下注(作用于开奖时触发=>action=start)
        self::automaticBet();
        //机器人随机进房
        self::randomIntoRoom();
    }

    /**
     * 机器人自动下注
     */
    private static function automaticBet()
    {
        Timer::add(self::$startRobotRandomBetRoomTime, function (){
            //开奖数据
            $openResult = self::$AllResultStatus;
            if($openResult) {
                //遍历所有台子
                foreach ($openResult as $showhao => $v) {
                    //根据showhao下注
                    if($v == 'start') {
                        //获取指定showhao的机器人用户
                        $allRobot = getValuesByField(self::$JiqirenUser, ['showhao' => $showhao]);

                        //如果这个房间没有机器人则直接跳出循环
                        if (empty($allRobot)) break;

                        $rank_key = array_rand($allRobot, 1);//随机机器人id
                        $robots = $allRobot[$rank_key];//单个机器人

                        if ($robotContents = $robots['content']) {//判断机器人是否有content
                            //开启概率投注
                            if(randomArray([
                                'submit'=>self::$successProbabilityBetRoom,
                                'cancel'=>self::$failProbabilityBetRoom
                            ])=='cancel') break;

                            //开启机器人下注(只执行一次)
                            \Workerman\Lib\Timer::add(self::$startRobotRandomBetRoomTime, function ($showhao) use ($robots, $robotContents) {

                                //获取随机的一条机器人内容
                                $getContent = getValuesByKeys($robotContents, strval(array_rand($robotContents, 1)));

                                $bet = [
                                    'showhao' => $showhao,
                                    'name' => $robots['name'],
                                    'portrait' => $robots['portrait'],
                                    'content' => $getContent['content'],
                                    'action' => 'betRoom',
                                ];
                                //存储投注信息
                                $bet['type'] = 'robot';
                                $bet['id'] = $robots['id'];
                                $bet['time'] = date('H:i:s');
                                //机器人发送信息
                                self::sendToHaveLoginUser($bet);

                                //加入投注信息
                                HandleRedis::setUserBet($showhao,$bet);

                                //加入聊天信息
                                HandleRedis::setChat($showhao,$bet);

                            }, [$showhao], false);//倒计时
                        }
                    }
                }
            }
        }, __FUNCTION__);
    }

    /**
     * 机器人随机进房
     */
    private static function randomIntoRoom()
    {
        //根据初始值开启机器人随机进房间操作
        Timer::add(self::$startRobotRandomIntoRoomTime, function () {
            //设置频率进入房间
            $off = intval((count(self::$JiqirenUser) * 3) / 10);
            for ($min = 0; $min < $off; $min++) {
                //设置随机值
                $randomIntoNum = rand(2, self::$randomIntoRoomTime);

                //随机机器人
                if(randomArray([
                        'submit'=>self::$successProbabilityIntoRoom,
                        'cancel'=>self::$failProbabilityIntoRoom
                    ])=='cancel') break;
                $randomRobot = rand(0, count(self::$JiqirenUser));

                //处理下标越位
                $getRobot = isset(self::$JiqirenUser[$randomRobot]) ? self::$JiqirenUser[$randomRobot] : [];

                //机器人存在则加入定时器
                if ($getRobot) {
                    \Workerman\Lib\Timer::add($randomIntoNum, function ($args = []) use ($getRobot) {
                        self::sendToHaveLoginUser([
                            'name' => $getRobot['name'],
                            'showhao' => $getRobot['showhao'],
                            'time' => date('H:i:s'),
//                            'room' => $getRobot['room'],
                            'action' => 'intoRoom'
                        ]);
                    }, [], false);
                }
            }
        }, __FUNCTION__);//把当前方法做key
    }
}
